import { LoginRequest } from './axios'
import { message } from 'antd';

// 获取二维码key
export function getQrCodeKey() {
  return LoginRequest({
    url: '/login/qr/key',
    params: {
      timerstamp: new Date().getTime(),
    }
  })
}

// 获取二维码
export function getQrCode(key) {
  return LoginRequest({
    url: '/login/qr/create',
    params: {
      key,
      timerstamp: new Date().getTime(),
      qrimg: true
    }
  })
}

// 轮询验证码状态800:等待扫描 801:过期 802:等待确认 803:成功返回cookie
export function getQrcodeStatus(key) {
  return LoginRequest({
    url: '/login/qr/check',
    params: {
      key,
      timerstamp: new Date().getTime(),
    }
  })
}

// 获取登录状态
export function getLoginStatus(cookie) {
  return LoginRequest({
    url: '/login/status',
    params: {
      cookie
    }
  })
}


export function AnonimouseLogin() {
  return LoginRequest({
    url: '/register/anonimous'
  })
}


// 发送验证码
export function getVerifyCode(phone) {
  return LoginRequest({
    url: '/captcha/sent',
    params: {
      phone
    }
  })
}

// 验证验证码
export function VerifyCode(phone, captcha) {
  return LoginRequest({
    url: '/captcha/verify',
    params: {
      phone,
      captcha
    }
  }).then(res => {
    message.config({
      top: 300,
    });
    message.error(res.response.data.message, 0);
  }).catch(err => {
    console.error(err.response, 'err');
  })
}

// 登录接口
export function LoginIn(param) {
  const { phone, password, captcha } = { ...param }
  return LoginRequest({
    url: '/login/cellphone',
    params: {
      phone,
      password,
      captcha
    }
  }).then(res => {
    if (res.code === 250) {
      message.config({
        top: 300,
      });
      message.error(res.message)
    }
  }).catch(err => {
    console.log('error');
  })
}